﻿@{
    ViewData["Title"] = ".NET Core 跨平台论坛";
    var user = ViewBag.User as ApplicationUser;
}
@model Topic
<div class="row">
    <div class="col-md-9 topics">
        <div class="panel panel-default" style="padding:15px">
            @if (user != null)
            {
                <form class="form-horizontal" method="post" asp-controller="Topic" asp-action="CreateOrUpdate">
                    <div class="form-group">
                        <label for="inputTitle" class="col-sm-2 control-label">版块</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" value="@ViewBag.NodeName">
                            <input type="text" id="NodeId" value="@ViewBag.NodeId" hidden="hidden">
                            @*<select asp-for="NodeId" asp-items="ViewBag.NodeListItem" class="form-control">
                                <option value="0">请选择版块</option>
                            </select>*@
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputTitle" class="col-sm-2 control-label">主题</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="inputTitle" placeholder="填写主题" required asp-for="Title">
                        </div>
                    </div>
                    @*<div class="form-group">
                        <label class="col-sm-2 control-label">内容</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" rows="10" asp-for="Content" placeholder="填写内容" id="editor" data-provide="markdown"></textarea>
                        </div>
                    </div>*@
                    <div class="form-group">
                        <label asp-for="Content" class="col-md-2 control-label"></label>
                        <div class="col-md-4" style="display:none;">
                            <textarea asp-for="Content" class="form-control"></textarea>
                            <span asp-validation-for="Content" class="text-danger"></span>
                        </div>
                        <div class="col-sm-10">
                            <script id="editor" type="text/plain" style="width:100%;height:300px;">
                            </script>
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="hidden" asp-for="UserId" value="@(user.Id)" />
                        <input type="hidden" asp-for="DepartmentId" value="@(user.DepartmentId)" />
                        <input type="hidden" asp-for="Email" value="@(user.Email)">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default col-md-2" onclick="summitContent()">发布</button>
                        </div>
                    </div>
                </form>
            }
            else
            {
                <a class="btn btn-primary btn-block" asp-controller="Account" asp-action="Login">发布新话题</a>
            }
        </div>

    </div>
    <div class="col-md-3">
        @Html.Partial("_SidebarPartial")
    </div>
</div>
@section scripts{
    <script type="text/javascript" charset="utf-8" src="~/lib/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="~/lib/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="~/lib/ueditor/lang/zh-cn/zh-cn.js"></script>

    <script type="text/javascript">
        function summitContent() {
            $("#Content").val(UE.getEditor('editor').getContent());
        }

        //实例化编辑器
        //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
        var ue = UE.getEditor('editor');


        function isFocus(e) {
            alert(UE.getEditor('editor').isFocus());
            UE.dom.domUtils.preventDefault(e)
        }
        function setblur(e) {
            UE.getEditor('editor').blur();
            UE.dom.domUtils.preventDefault(e)
        }
        function insertHtml() {
            var value = prompt('插入html代码', '');
            UE.getEditor('editor').execCommand('insertHtml', value)
        }
        function createEditor() {
            enableBtn();
            UE.getEditor('editor');
        }
        function getAllHtml() {
            alert(UE.getEditor('editor').getAllHtml())
        }
        function getContent() {
            var arr = [];
            arr.push("使用editor.getContent()方法可以获得编辑器的内容");
            arr.push("内容为：");
            arr.push(UE.getEditor('editor').getContent());
            alert(arr.join("\n"));
        }
        function getPlainTxt() {
            var arr = [];
            arr.push("使用editor.getPlainTxt()方法可以获得编辑器的带格式的纯文本内容");
            arr.push("内容为：");
            arr.push(UE.getEditor('editor').getPlainTxt());
            alert(arr.join('\n'))
        }
        function setContent(isAppendTo) {
            var arr = [];
            arr.push("使用editor.setContent('欢迎使用ueditor')方法可以设置编辑器的内容");
            UE.getEditor('editor').setContent('欢迎使用ueditor', isAppendTo);
            alert(arr.join("\n"));
        }
        function setDisabled() {
            UE.getEditor('editor').setDisabled('fullscreen');
            disableBtn("enable");
        }

        function setEnabled() {
            UE.getEditor('editor').setEnabled();
            enableBtn();
        }

        function getText() {
            //当你点击按钮时编辑区域已经失去了焦点，如果直接用getText将不会得到内容，所以要在选回来，然后取得内容
            var range = UE.getEditor('editor').selection.getRange();
            range.select();
            var txt = UE.getEditor('editor').selection.getText();
            alert(txt)
        }

        function getContentTxt() {
            var arr = [];
            arr.push("使用editor.getContentTxt()方法可以获得编辑器的纯文本内容");
            arr.push("编辑器的纯文本内容为：");
            arr.push(UE.getEditor('editor').getContentTxt());
            alert(arr.join("\n"));
        }
        function hasContent() {
            var arr = [];
            arr.push("使用editor.hasContents()方法判断编辑器里是否有内容");
            arr.push("判断结果为：");
            arr.push(UE.getEditor('editor').hasContents());
            alert(arr.join("\n"));
        }
        function setFocus() {
            UE.getEditor('editor').focus();
        }
        function deleteEditor() {
            disableBtn();
            UE.getEditor('editor').destroy();
        }
        function disableBtn(str) {
            var div = document.getElementById('btns');
            var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
            for (var i = 0, btn; btn = btns[i++];) {
                if (btn.id == str) {
                    UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
                } else {
                    btn.setAttribute("disabled", "true");
                }
            }
        }
        function enableBtn() {
            var div = document.getElementById('btns');
            var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
            for (var i = 0, btn; btn = btns[i++];) {
                UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
            }
        }

        function getLocalData() {
            alert(UE.getEditor('editor').execCommand("getlocaldata"));
        }

        function clearLocalData() {
            UE.getEditor('editor').execCommand("clearlocaldata");
            alert("已清空草稿箱")
        }

        //当ueditor加载完成后，就把Description里的内容放到编辑器中，实现update功能
        $(document).ready(function () {
            UE.getEditor('editor').addListener("ready", function () {
                // editor准备好之后才可以使用
                var str = $("#Content").val();
                UE.getEditor('editor').setContent(str);
            });
        });
    </script>
}

